﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq.Mapping;
using System.ComponentModel.DataAnnotations;

namespace DomainModel
{
    [Table(Name = "Users")]
    public class User
    {
        [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
        public Int32 UserID { get; set; }

        [Column]
        [Required(ErrorMessage="*")]
        public String Login { get; set; }

        [Column]
        [Required(ErrorMessage="*")]
        public String Password { get; set; }

        [Column]
        [Required(ErrorMessage="*")]
        [RegularExpression(@"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", ErrorMessage = "*")]
        public String Email { get; set; }


        [Column]
        [Required(ErrorMessage = "*")]
         public String Organization { get; set; }

        /// <summary>
        /// Фамилия
        /// </summary>
        [Column]
        [Required(ErrorMessage="*")]
        public String Surname { get; set; }

        [Column]
        [Required(ErrorMessage="*")]
        public String Name { get; set; }

        [Column]
        [Required(ErrorMessage="*")]
        public String Patro { get; set; }

        [Column]
        [Required(ErrorMessage = "*")]
        public String Phone { get; set; }

        [Column]
        public Decimal Ratio { get; set; }


        [Column]
        public bool isFinVisible { get; set; }


        [Column]
        public Int32 rf_RoleID { get; set; }
        
        [Column]
        public Int32 rf_UserID { get; set; }

        public string FIO {
            get { return string.Format("{0} {1} {2}",Surname,Name,Patro);}
            set
            {
                string[] ss = value.Split(' ');
                Surname = ss[0];
                Name = ss[1];
                string s = string.Empty;
                for (int i = 2; i < ss.Length; i++)
                {
                    s += ss[i] + ' ';
                }
                Patro = s.Trim();
            }
        }

        [Required(ErrorMessage = "*")]
        [RegularExpression(@"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", ErrorMessage = "*")]
        public string ManagerMail { get; set; }

        public bool IsAdmin 
        {
            get { return rf_RoleID == 1;} 
        }

        public bool IsManager
        {
            get { return rf_RoleID == 2; }
        }

        public bool IsClient
        {
            get { return rf_RoleID == 3; }
        }
        
        public User()
        {            
        }
    }
}
